function TagsCloud(div, tags) {

	var self = this;
	this.tags = tags();
	this.selectedIds = new Array();

	this.init = function() {
		this.tags.sort(function(t1, t2) {
			return t1.name == t2.name ? 0 : (t1.name < t2.name ? -1 : 1);
		});
		this.render();
	};

	this.render = function() {
		var html = '<div class="tag-cloud">';
		var spans = [];
		var minFreq = 0;
		var maxFreq = 0;
		this.tags.forEach(function(tag) {
			var divId = div.slice(1) + '_' + tag.id;
			spans.push('<span class="tag" id="' + divId + '">' + tag.name + '</span>');
			minFreq = Math.min(minFreq, tag.frequency);
			maxFreq = Math.max(maxFreq, tag.frequency);
		});
		html += spans.join('') + '</div>';
		$(div).html(html);
		var fontSize;
		this.tags.forEach(function(tag) {
			var divId = div + '_' + tag.id;
			$(divId).on('click', function() {
				self.toggleTag(tag.id);
			});
			fontSize = 80 + 70 * (tag.frequency - minFreq) / (maxFreq - minFreq);
			$(divId).css({ 'font-size': fontSize + '%'});
		});
	};

	this.toggleTag = function(id) {
		if($.inArray(id, this.selectedIds) < 0) {
			this.selectTag(id);
		} else {
			this.unselectTag(id);
		}
	};

	this.selectTag = function(id) {
		var divId = div + '_' + id;
		this.selectedIds.push(id);
		$(divId).attr('class', 'tag-selected');
	};

	this.unselectTag = function(id) {
		var divId = div + '_' + id;
		this.selectedIds.splice($.inArray(id, this.selectedIds), 1);
		$(divId).attr('class', 'tag');
	};
};